#ifndef THREAD_GUARD_H_
#define THREAD_GUARD_H_
#include <thread>
#include <iostream>

// RAII 资源获取即初始化，利用异常抛出时临时变量也会被回收机制将资源的操作写到析构函数中去
class thread_gurad {
    private:
    std::thread &_t;

    public:
    explicit thread_gurad(const thread_gurad  &t) = delete;
    thread_gurad & operator =(const thread_gurad  &t) = delete;
    // binding reference of type 'std::thread' to value of type 'const std::thread' drops 'const' qualifier
    thread_gurad(std::thread &t):_t(t){};
    ~thread_gurad(){
        if (_t.joinable())
        {
            std::cout<< "thread_guard: 调用了join函数" << std::endl;
            _t.join();
        }
    }
};

#endif